var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/Attachment/views/tableAttachment/index.tsx
var tableAttachment_exports = {};
__export(tableAttachment_exports, {
  OriginTableAttachment: () => OriginTableAttachment,
  TableAttachment: () => TableAttachment
});
module.exports = __toCommonJS(tableAttachment_exports);
var import_react = require("react");
var import_functionalComponent = require("../../../../functionalComponent");
var import_util = require("../../../../util");
var import_AttachmentContext = require("../../AttachmentContext");
var import_components = require("../../components");
var import_util2 = require("../../util");
var import_index = require("./index.less");
var attachmentCategoryOptions = [
  { label: "单据附件", key: "attach" },
  { label: "审批后附件", key: "approvedAttach" }
  // { label: '工作流附件', value: 'workFlowAttach' },
  // { label: '来源单据附件', value: 'oriBizAttach' },
];
var OriginTableAttachment = (props) => {
  var _a;
  const { style = {}, categoryStyle, tableStyle = {}, uploadRef = (0, import_react.useRef)(null), labelConfig } = props;
  const {
    beforeUploadValidate,
    accept,
    maxCount,
    disabled,
    actions,
    permission,
    attachmentRecordList,
    loading,
    isReady,
    attachmentFileList,
    approved,
    uploading,
    onUploadStateChange,
    onContentChanged
  } = (0, import_AttachmentContext.useAttachment)();
  const { ins, params } = (0, import_react.useContext)(import_functionalComponent.ModalContext);
  const [attachType, setAttachType] = (0, import_react.useState)("attach");
  const [checkedListMap, setCheckedListMap] = (0, import_react.useState)({
    attach: [],
    approvedAttach: []
  });
  const checkedList = (0, import_react.useMemo)(() => checkedListMap[attachType], [checkedListMap, attachType]);
  const [curRow, setCurRow] = (0, import_react.useState)();
  const [remarkVisible, setRemarkVisible] = (0, import_react.useState)(false);
  const [downloadRecordVisible, setDownloadRecordVisible] = (0, import_react.useState)(false);
  const _tableConfig = [
    {
      title: "附件名称",
      dataIndex: "asrName",
      width: 210,
      flex: 1,
      tooltip: true,
      filter: true,
      render: ({ row }) => {
        const isFail = row.status === "fail";
        const uploaded = row.status === "done" || row.status === void 0;
        const isPreview = uploaded && (permission == null ? void 0 : permission.preview) !== 2 && actions.isPreview(row.asrName || row.fileName);
        return /* @__PURE__ */ React.createElement("span", { className: `attachment-flex-center ${isFail ? "upload-failed" : ""}` }, /* @__PURE__ */ React.createElement(
          import_components.FileView,
          {
            iconPosition: "right",
            isPreview,
            data: {
              fileName: row.asrName || row.fileName,
              fileSize: row.originFileSize || row.fileSize,
              status: row.status,
              percent: row.percent
            },
            handlePreview: () => {
              if ((permission == null ? void 0 : permission.preview) === 0) {
                import_functionalComponent.message.error("没有预览权限，无法预览");
                return;
              }
              row.asrFid && actions.preview(row.asrFid);
            }
          }
        ));
      }
    },
    {
      title: "附件状态",
      dataIndex: "asrFlag",
      width: 80,
      flex: 1,
      render: ({ value }) => ["未保存", "已保存"][+value]
    },
    {
      title: "标签",
      dataIndex: "label",
      width: ((_a = labelConfig == null ? void 0 : labelConfig.style) == null ? void 0 : _a.width) || 150,
      flex: 1,
      render: ({ value, row }) => {
        return /* @__PURE__ */ React.createElement(
          import_components.LabelSelect,
          {
            labelConfig: {
              ...labelConfig,
              defaultValue: value,
              disabled,
              style: { ...labelConfig == null ? void 0 : labelConfig.style, width: "100%" }
            },
            fileId: row.asrFid,
            onChangeCallback: onContentChanged
          }
        );
      }
    },
    {
      title: "备注",
      dataIndex: "asrRemark",
      width: 80,
      flex: 1,
      tooltip: true
    },
    {
      title: "附件大小",
      dataIndex: "originFileSize",
      width: 80,
      format: {
        type: "number",
        formatter: ({ value }) => (0, import_util2.getFileSize)(value)
      },
      flex: 1
    },
    {
      title: "上传人",
      dataIndex: "userName",
      width: 80,
      flex: 1
    },
    {
      title: "上传时间",
      dataIndex: "asrFilldt",
      width: 150,
      flex: 1,
      render: ({ value }) => value && import_util.iup.formatDate(new Date(value), "YYYY-MM-DD HH:mm:ss")
    },
    // {
    //   title: '共享方式',
    //   dataIndex: 'shareType',
    //   width: 80,
    //   flex: 1,
    //   render: ({ value, row }) => (
    //     <Select
    //       className="share-type-select"
    //       options={shareTypeOptions}
    //       defaultValue={value && value.split(',')[0]}
    //       bordered={false}
    //       onSelect={(v) => handleChangeShareType(v, row).then(getTableAttach)}
    //     />
    //   )
    // },
    // {
    //   title: '共享给',
    //   dataIndex: 'sharedOneName',
    //   width: 80,
    //   flex: 1,
    //   tooltip: true
    // },
    {
      title: "操作",
      dataIndex: "options",
      flex: 1,
      width: 150,
      fixed: "right",
      render: ({ row }) => {
        const uploaded = row.status === "done" || row.status === void 0;
        if (!uploaded)
          return null;
        return /* @__PURE__ */ React.createElement(import_functionalComponent.Space, { size: 0, split: /* @__PURE__ */ React.createElement(import_functionalComponent.Divider, { type: "vertical", style: { borderLeft: "1px solid var(--primary-color)" } }) }, /* @__PURE__ */ React.createElement(
          "a",
          {
            style: { color: "var(--primary-color)" },
            onClick: () => {
              setCurRow(row);
              setRemarkVisible(true);
            }
          },
          "修改备注"
        ), /* @__PURE__ */ React.createElement(
          "a",
          {
            style: { color: "var(--primary-color)" },
            onClick: () => {
              setCurRow(row);
              setDownloadRecordVisible(true);
            }
          },
          "下载记录"
        ));
      }
    }
  ];
  const filterAttachmentCategoryOptions = (0, import_react.useMemo)(() => {
    if (+approved === 1 || (attachmentRecordList == null ? void 0 : attachmentRecordList.findIndex((item) => +item.approved === 1)) > -1) {
      return attachmentCategoryOptions;
    } else {
      return attachmentCategoryOptions.filter((item) => item.key !== "approvedAttach");
    }
  }, [approved, attachmentRecordList]);
  const onToolbarClick = (0, import_util.useRefCallback)((action) => {
    switch (action) {
      case "delete":
        if (!checkedList.length) {
          import_functionalComponent.message.warning("请选择至少一行进行删除");
          return;
        }
        if (attachType !== "approvedAttach" && +approved === 1) {
          import_functionalComponent.message.error("已审批的单据只能删除审批后附件");
          return;
        }
        actions.delete(checkedList);
        break;
      case "download":
        if (!checkedList.length) {
          import_functionalComponent.message.warning("请选择至少一行进行下载");
        } else {
          actions.download(checkedList);
        }
        break;
      case "zipDownload":
        actions.zipDownload();
        break;
    }
  });
  const dataSource = (0, import_react.useMemo)(() => {
    const _checkedList = checkedListMap[attachType];
    const checkedNames = _checkedList.map((item) => item.fileName);
    return attachmentFileList.map((v) => {
      return {
        ...v,
        checked: checkedNames.includes(v.fileName)
      };
    });
  }, [attachmentFileList, checkedListMap, attachType]);
  const tableConfig = import_util.iup.isNullOrEmpty(labelConfig) ? _tableConfig.filter((col) => col.dataIndex !== "label") : _tableConfig;
  return /* @__PURE__ */ React.createElement(import_util.Layout, { className: "iup-table-attachment", style, direction: "row" }, /* @__PURE__ */ React.createElement(import_util.Layout.Flex, { className: "iup-table-attachment-content" }, /* @__PURE__ */ React.createElement(
    import_components.Toolbar,
    {
      accept,
      uploadRef,
      uploading,
      permission,
      onUploadStateChange,
      onUploaded: onContentChanged,
      onBeforeUpload: beforeUploadValidate,
      isReady,
      onToolbarClick
    }
  ), /* @__PURE__ */ React.createElement("div", { style: { margin: "5px 0 " } }), filterAttachmentCategoryOptions.length > 1 ? /* @__PURE__ */ React.createElement(
    import_functionalComponent.Tabs,
    {
      fitHeight: false,
      size: "small",
      defaultActiveKey: filterAttachmentCategoryOptions[0].key,
      items: filterAttachmentCategoryOptions,
      onChange: (v) => setAttachType(v),
      tabBarStyle: { backgroundColor: "#F9FAFB", padding: "0 22px 3px" }
    }
  ) : null, /* @__PURE__ */ React.createElement(import_util.Layout.Flex, { className: "attachment-table-container", style: tableStyle }, /* @__PURE__ */ React.createElement(
    import_functionalComponent.Table,
    {
      remember: false,
      key: attachType,
      checkbox: true,
      headerMenu: false,
      rowHeight: 40,
      loading,
      onCheckedChange: (list) => setCheckedListMap((state) => ({ ...state, [attachType]: list })),
      columns: tableConfig,
      dataSource
    }
  )), (params == null ? void 0 : params.isModal) ? /* @__PURE__ */ React.createElement("div", { style: { display: "flex", margin: "15px 5px 5px", justifyContent: "flex-end" } }, /* @__PURE__ */ React.createElement(import_functionalComponent.Button, { onClick: () => params == null ? void 0 : params.handleCancel(ins), style: { width: 84, marginRight: 10 } }, "取消"), /* @__PURE__ */ React.createElement(import_functionalComponent.Button, { onClick: () => params == null ? void 0 : params.handleOk(ins), type: "primary", style: { width: 84 } }, "确定")) : null), /* @__PURE__ */ React.createElement(
    import_components.RemarkModal,
    {
      data: curRow,
      visible: remarkVisible,
      disabled,
      onCancel: () => setRemarkVisible(false),
      onSaved: onContentChanged
    }
  ), /* @__PURE__ */ React.createElement(import_components.DownloadRecordModal, { data: curRow, visible: downloadRecordVisible, setVisible: setDownloadRecordVisible }));
};
var TableAttachment = (0, import_react.forwardRef)(
  ({ tableStyle, categoryStyle, style, labelConfig, uploadRef, ...rest }, ref) => {
    return /* @__PURE__ */ React.createElement(import_AttachmentContext.AttachmentProvider, { ...rest, ref }, /* @__PURE__ */ React.createElement(
      OriginTableAttachment,
      {
        style,
        tableStyle,
        uploadRef,
        categoryStyle,
        labelConfig
      }
    ));
  }
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  OriginTableAttachment,
  TableAttachment
});
